package com.atguigu.spzx.product.service.impl;

import com.atguigu.spzx.model.pojo.product.Category;
import com.atguigu.spzx.product.mapper.CategoryMapper;
import com.atguigu.spzx.product.service.CategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
@Transactional
@Slf4j
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /*
     * @description:  获取一级分类
     **/
    @Override
    @Cacheable(value = "category:one", key = "'all'")
    public List<Category> getFirstCategory() {
        return categoryMapper.findFirstCategory();
    }


//    @Override
//    public List<Category> findCategoryTree() {

    /// /        1、查看redis中是否有值
//        String categoryListJSON = redisTemplate.opsForValue().get("category:one::all");
//        // redis 中有值
//        if (StringUtils.isNotEmpty(categoryListJSON)) {
//            List<Category> categoryList = JSON.parseArray(categoryListJSON, Category.class);
//            log.info("redis中成功获取到分类结果");
//            return categoryList;
//        }
//
//        //        2、如果没有就查找数据库
//        List<Category> categories = categoryMapper.selectList(null);
//        List<Category> categoryTree = new ArrayList<>();
//        for (Category category : categories) {
//            if (category.getParentId() == 0) {
//                treeHelper(category, categories);
//                categoryTree.add(category);
//            }
//        }
//        log.info("从数据库中查询到结果并存入redis");
//
//        //        3、存入redis
//        redisTemplate.opsForValue().set("category:one::all", JSON.toJSONString(categories));
//        return categoryTree;
//    }
    @Override
    @Cacheable(value = "category:one", key = "'tree'")
    public List<Category> findCategoryTree() {
        List<Category> categories = categoryMapper.selectList(null);
        List<Category> categoryTree = new ArrayList<>();
        for (Category category : categories) {
            if (category.getParentId() == 0) {
                treeHelper(category, categories);
                categoryTree.add(category);
            }
        }
        return categoryTree;
    }

    @Cacheable(value = "category", key = "'all'")
    @Override
    public List<Category> findAllCategory() {
        return categoryMapper.selectList(null);
    }


    /*
     * @description: 递归查找分类树
     **/
    private void treeHelper(Category category, List<Category> categories) {
        for (Category category1 : categories) {
            if (Objects.equals(category1.getParentId(), category.getId())) {
                if (category.getChildren() == null) {
                    category.setChildren(new ArrayList<>());
                }
                category.getChildren().add(category1);
                treeHelper(category1, categories);
            }
        }
        // 没有子分类就设置没有孩子字段
        if (category.getChildren() == null) {
            category.setHasChildren(false);
        }
    }
}
